#!/bin/bash
# Test forever. Or not, if something goes wrong.

run=1
i=1
param=${1-"none"}

# Function that put all output lines in the same order
function unificate {
	for i in $(cat $1)
	do 
		echo $i | tr "-" "\n" | sort | tr "\n" "-" | sed 's/-$/\n/'
	done | sort
}

# Just print a line
function line {
	echo '-------------------------------------------------'
}

while (( $run ))
do
	echo '***********************************************'
	echo "Making test number $i..."
	echo '***********************************************'
	
	run=0
	
	# Generate input
	input=$(mktemp)
	ruby input.rb > $input
	if [ $param == '--debug' ]; then line; echo "Random input generated"; line; cat $input; fi
	
	# Test with other programs
	tmpfile=$(mktemp)
	
	# One
	( ruby chordal.rb < $input ) > $tmpfile
	if [ $param == '--debug' ]; then line; echo "Caio's original output"; line; cat $tmpfile; fi
	first=$(mktemp)
	unificate $tmpfile > $first
	if [ $param == '--debug' ]; then line; echo "Caio's sorted output"; line; cat $first; fi
	
	# And the other
	( python ../SVN/EfraimLucasPaulo/grafo2.py < $input ) > $tmpfile
	if [ $param == '--debug' ]; then line; echo "Conf's original output"; line; cat $tmpfile; fi
	second=$(mktemp)
	unificate $tmpfile > $second
	if [ $param == '--debug' ]; then line; echo "Conf's sorted output"; line; cat $second; fi
	
	# Compare both
	diff=$(mktemp)
	( diff -q $first $second ) > $diff
	
	# Check if output is greater than one byte
	if [ -s $diff ]
	then
		
		run=0
	 	echo '----------------------------------------'	
		diff -y $first $second
	 	echo '----------------------------------------'	
		echo "E R R O R. See differences above. Abort."
		exit 0

	else
		run=1
	fi

	cat $diff

	echo "Test passed!"

	i=$((i+1))

	rm -f $first $second $input $diff $tmpfile

done
